/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.neuralstudio.utils;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.util.List;
import org.netbeans.api.visual.action.ActionFactory;
import org.netbeans.api.visual.anchor.AnchorFactory;
import org.netbeans.api.visual.anchor.AnchorShape;
import org.netbeans.api.visual.graph.GraphScene;
import org.netbeans.api.visual.widget.ConnectionWidget;
import org.netbeans.api.visual.widget.LayerWidget;
import org.netbeans.api.visual.widget.Scene;
import org.netbeans.api.visual.widget.Widget;
//import org.neuralStudio.editor.palette.Object;

/**
 *
 * @author william
 */
public class GraphSceneImpl extends GraphScene {
    private LayerWidget mainLayer;
    private LayerWidget connectionLayer;
    private int n = 5; //space between nodes
    private int r = 20; //node radius
    private int a = 200; // midline
    private int x = 20; // initial x
    private Color nodeColor;

    //private Image nodeImage;


    public GraphSceneImpl() {
        this.mainLayer = new LayerWidget(this);
        this.connectionLayer = new LayerWidget(this);
        this.addChild(this.mainLayer);
        this.addChild(this.connectionLayer);
        /*
        this.addNode(1);
        this.addNode(2);
        Widget w1 = this.mainLayer.getChildren().get(0);
        Widget w2 = this.mainLayer.getChildren().get(1);
        this.connectNodes(w1, w2);
         *
         */

        //this.nodeImage = ImageUtilities.loadImage("org/electre/resources/node.jpg");
        /*
        this.getActions().addAction(ActionFactory.createAcceptAction(new AcceptProvider() {

            public ConnectorState isAcceptable(Widget widget, Point point, Transferable t) {
                Image dragImage = getImageFromTransferable(t);
                JComponent view = getView();
                Graphics2D g = (Graphics2D)view.getGraphics();
                Rectangle visRect = view.getVisibleRect();
                view.paintImmediately(visRect.x, visRect.y, visRect.width, visRect.height);
                g.drawImage(dragImage,AffineTransform.getTranslateInstance(point.getLocation().getX(),point.getLocation().getY()),null);
                return ConnectorState.ACCEPT;
            }

            public void accept(Widget widget, Point point, Transferable t) {
               Image image = getImageFromTransferable(t);
                Widget w = NetworkSceneImpl.this.addNode(new Object(image));
                w.setPreferredLocation(widget.convertLocalToScene(point));
            }

        }));
         *
         */
    }
    /*
    public Image getImageFromTransferable(Transferable t) {
        Object o = null;
        try {
            o = t.getTransferData(DataFlavor.imageFlavor);
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (UnsupportedFlavorException ex) {
            ex.printStackTrace();
        }
        return o instanceof Image ? (Image) o : ImageUtilities.loadImage("org/neuralStudio/editor/palette/FunctionDS.jpg");
    }
    */

    public void paintLayer(int N, Color color) {
        //N number of nodes
        // x x-coordinate
        this.nodeColor = color;
        this.x += 150;
        int p;
        int halfn = N/2;
        p = a - ((N-1)*r + ((N-1)/2)*n);
        /*
        if ((N % 2) == 0) {
            //p = a - (((N-1)/2)*n + (N-2)*r +r/2);
            //p = a - (((N/2)-1)*n + N*r);
            p = a - ((N-1)*r + ((N-1)/2)*n);
        }else{
            //p = a - (halfn*n + (N-1)*r);
            p = a - ((N-1)*r + );
        }
         *
         */
        for (int i = 0; i < N; i++) {
            addNode(p);
            p += 2*r + n;
        }
     }

    public void connectLayers(int[] data) {
        int start1 = 0;
        int start2 = data[0];
        List<Widget> widgets = mainLayer.getChildren();
        for (int layer = 0; layer < data.length-1; layer++) {
            //int start = (layer == 0)?0:data[layer-1]+1;
            for (int j = start1; j < start1+data[layer]; j++) {
                for (int k = start2; k < start2 + data[layer+1]; k++) {
                    this.connectNodes(widgets.get(j),widgets.get(k));
                }
            }
            start1 += data[layer];
            start2 += data[layer+1];
        }
    }

    private void connectNodes(Widget source, Widget target) {
        ConnectionWidget conn = new ConnectionWidget(this);
        conn.setTargetAnchorShape(AnchorShape.TRIANGLE_FILLED);
        conn.setTargetAnchor(AnchorFactory.createRectangularAnchor(target));
        conn.setSourceAnchor(AnchorFactory.createRectangularAnchor(source));
        connectionLayer.addChild(conn);
    }

    @Override
    protected Widget attachNodeWidget(Object node) {
        Integer y = (Integer)node;
        CircleWidget widget = new CircleWidget(this,r);
        widget.getActions().addAction(ActionFactory.createMoveAction());
        widget.setPreferredLocation(new Point(x,y));
        mainLayer.addChild(widget);
        return widget;
    }

    @Override
    protected Widget attachEdgeWidget(Object e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void attachEdgeSourceAnchor(Object e, Object n, Object n1) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void attachEdgeTargetAnchor(Object e, Object n, Object n1) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    private class CircleWidget extends Widget {
        private int radius;
        public CircleWidget (Scene scene, int radius) {
            super (scene);
            this.radius = radius;

        }
        @Override
        protected Rectangle calculateClientArea () {
            return new Rectangle (- radius, - radius, 2 * radius + 1, 2 * radius + 1);
        }
        @Override
        protected void paintWidget () {
            Graphics2D g = getGraphics ();
            g.setColor (GraphSceneImpl.this.nodeColor);
            g.fillOval (- radius, - radius, 2 * radius, 2 * radius);

        }
    }

    public void setX(int x) {
        this.x = x;
    }







/*
    private class MyConnectProvider implements ConnectProvider{

        public boolean isSourceWidget(Widget source) {
            return source instanceof IconNodeWidget && source != null? true : false;
        }

        public ConnectorState isTargetWidget(Widget src, Widget trg) {
            return src != trg && trg instanceof IconNodeWidget ? ConnectorState.ACCEPT : ConnectorState.REJECT;
        }

        public boolean hasCustomTargetWidgetResolver(Scene arg0) {
            return false;
        }

        public Widget resolveTargetWidget(Scene arg0, Point arg1) {
            return null;
        }

        public void createConnection(Widget source, Widget target) {
            ConnectionWidget conn = new ConnectionWidget(GraphSceneImpl.this);
            conn.setTargetAnchorShape(AnchorShape.TRIANGLE_FILLED);
            conn.setTargetAnchor(AnchorFactory.createRectangularAnchor(target));
            conn.setSourceAnchor(AnchorFactory.createRectangularAnchor(source));
            connectionLayer.addChild(conn);
        }
    }
*/
}